local skel = fk.CreateSkill {
  name = "rmt__feibi",
  tags = { Skill.Combo },
  dynamic_desc = function (self, player, lang)
    local banner = Fk:currentRoom():getBanner("rmt__feibi_combos")
    if banner == nil then return self.name end
    --[].连招条件序号 1.连招条件（1~8） 2.是否已满足（1/2) 3.连招技的拥有者（返回ID）
    local info = "连招技（"
    for loop = 0, 10086, 1 do 
      local name = "rmt__feibi"..loop
      if banner and banner[name] and banner[name][3] == player.id then
        if info == "连招技（" then
          if banner[name][2] == 2 then
          info = info.."<font color=\'green'>"..Fk:translate("feibi"..banner[name][1]).."</font>"
          elseif banner[name][2] == 1 then
          info = info..Fk:translate("feibi"..banner[name][1])
          end
        else
          if banner[name][2] == 2 then
          info = info.."+<font color=\'green'>"..Fk:translate("feibi"..banner[name][1]).."</font>"
        elseif banner[name][2] == 1 then
          info = info.."+"..Fk:translate("feibi"..banner[name][1])
          end
        end
      end
    end
    return info.."），你可于此牌结算后隐匿，若如此做，跳过你下个弃牌阶段，将你使用的下一张牌的类型加入连招条件；"..
    "若类型为装备牌，你登场并视为倒序使用其余连招条件（为类型的条件改为你视为使用任意同类型牌），最后清除所有非【杀】条件。"
  end
}

local U = require "packages.utility.utility"
local DIY = require "packages.diy_utility.diy_utility"

---@param i integer @ 连招条件对应的数字 1:基本牌 2:锦囊牌 3:装备牌 4:【杀】
---@param card Card @ 使用的牌
--匹配飞匕展示的牌与所有连招条件的符合情况
local function feibiCheck(i, card)
  if i == 1 then
    return card.type == Card.TypeBasic
  elseif i == 2 then
    return card.type == Card.TypeTrick
  elseif i == 3 then
    return card.type == Card.TypeEquip
  elseif i == 4 then
    return card.trueName == "slash"
  end
end

Fk:loadTranslationTable{
  ["feibi1"] = "基本牌",
  ["feibi2"] = "锦囊牌",
  ["feibi3"] = "装备牌",
  ["feibi4"] = "【杀】",
  ["<font color=\'green'>feibi1</font>"] = "<font color=\'green'>基本牌</font>",
  ["<font color=\'green'>feibi2</font>"] = "<font color=\'green'>锦囊牌</font>",
  ["<font color=\'green'>feibi3</font>"] = "<font color=\'green'>装备牌</font>",
  ["<font color=\'green'>feibi4</font>"] = "<font color=\'green'>【杀】</font>",
}

skel:addEffect(fk.CardUseFinished, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and
    data.extra_data and data.extra_data.combo_skill and data.extra_data.combo_skill[skel.name]
  end,
  on_cost = function (self, event, target, player, data)
    local banner = player.room:getBanner("rmt__feibi_combos") or {}
    local invoke = player.room:askToSkillInvoke(player, { skill_name = skel.name })
      if invoke then
        for loop = 0, 10086, 1 do
          local name = "rmt__feibi"..loop
          if banner and banner[name] and banner[name][3] == player.id then
            banner[name][2] = 1
            player.room:setBanner("rmt__feibi_combos", banner)
          end
        end
        return true
      end
    end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("__hidden_general") == 0 and player:getMark("__hidden_deputy") == 0 then
      DIY.enterHidden(player)
    end
    room:setPlayerMark(player, "@@rmt__feibi_add", 1)
    room:setPlayerMark(player, "@@rmt__feibi_skip", 1)
  end,
})

skel:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, true) and data.card
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local banner = room:getBanner("rmt__feibi_combos") or {}
    if banner == {} or banner == nil then return false end
    local combodone = true
    for loop = 0, 10086, 1 do
      local name = "rmt__feibi"..loop
      local lastnum = loop > 1 and loop - 1 or 1
      local lastname = "rmt__feibi"..lastnum
      if banner and banner[name] and banner[name][3] == player.id and banner[name][2] == 1 then --优先匹配第一个未完成的
        if feibiCheck(banner[name][1], data.card) == true then
          banner[name][2] = 2
          player.room:setBanner("rmt__feibi_combos", banner)
        elseif feibiCheck(banner[name][1], data.card) == false and feibiCheck(banner[lastname][1], data.card) == false then --当前和上一个都不符合的话打断
          for i = 0, 10086, 1 do
            local iname = "rmt__feibi"..i
            if banner and banner[iname] and banner[iname][3] == player.id then
              banner[iname][2] = 1
              player.room:setBanner("rmt__feibi_combos", banner)
            end
          end
        end
        break
      end
    end
    for loop = 0, 10086, 1 do --判断完当前使用牌的进度符合条件以后再逐个寻找连招技进度
      local name = "rmt__feibi"..loop
      if banner and banner[name] and banner[name][3] == player.id then
        if banner[name][2] == 1 then
          combodone = false
          break
        end
      end
    end
    if combodone then
      data.extra_data = data.extra_data or {}
      data.extra_data.combo_skill = data.extra_data.combo_skill or {}
      data.extra_data.combo_skill[skel.name] = true
    return
    end
  end,
})

skel:addEffect(fk.EventPhaseChanging, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return
      target == player and
      data.phase == Player.Discard and
      not data.skipped and
      target:getMark("@@rmt__feibi_skip") > 0
  end,
  on_cost = Util.TrueFunc,
  on_trigger = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@rmt__feibi_skip", 0)
    data.skipped = true
  end,
})

skel:addEffect(fk.AfterCardUseDeclared, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    local add = false
    local infos = target.room:getBanner("rmt__feibi_combos")
    for loop = 1, 10086, 1 do
      local name = "rmt__feibi"..loop
      if infos and infos[name] and infos[name][3] == target.id then --已有记录才能添加连招
        add = true
        break
      end
    end
    return target == player and data.card and target:getMark("@@rmt__feibi_add") > 0 and add
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = target.room
    player:broadcastSkillInvoke("rmt__feibi")
    room:setPlayerMark(target, "@@rmt__feibi_add", 0)
    local banner = room:getBanner("rmt__feibi_combos") or {}
    if banner == {} or banner == nil then return false end
    local type
    if data.card and data.card.type == Card.TypeBasic then
      type = 1
    elseif data.card and data.card.type == Card.TypeTrick then
      type = 2
    elseif data.card and data.card.type == Card.TypeEquip then
      type = 3
    end
    local name
    local x = 0
    for i = 1, 10086, 1 do
      if banner["rmt__feibi"..tostring(i)] == nil then
        name = "rmt__feibi"..tostring(i)
        banner[name] = {
          type, -- 1:基本牌 2:锦囊牌 3:装备牌 4:【杀】
          1, -- 1:未满足 2:已满足
          target.id,
        }
        room:addPlayerMark(target, "@rmt__feibi", 1)
        room:setBanner("rmt__feibi_combos", banner)
        x = x + 1
        if x > 0 then break end
      end
    end
    if type == 3 then
      if not target.dead then
        if target:getMark("__hidden_general") ~= 0 or target:getMark("__hidden_deputy") ~= 0 then
          U.GeneralAppear(player, skel.name)
        end
        for loop = 10086, 1, -1 do
          name = "rmt__feibi"..loop
          if banner and banner[name] and banner[name][3] == target.id then
            banner[name][2] = 1 --先清除所有连招满足的进度
            room:setBanner("rmt__feibi_combos", banner)
          end
        end
        for loop = 10086, 1, -1 do
          if target.dead then break end
          local name = "rmt__feibi"..loop
          if banner and banner[name] and banner[name][3] == target.id then
            local cards
            if banner[name][1] == 1 then
              cards = U.getUniversalCards(room, "b", false)
            elseif banner[name][1] == 2 then
              cards = U.getUniversalCards(room, "t", false)
            elseif banner[name][1] == 4 then
              room:askToUseVirtualCard(player, {
                name = "slash",
                skill_name = skel.name,
                cancelable = false,
                extra_data = {
                  bypass_times = true,
                  extraUse = true,
                },
              })
            end
            if cards ~= nil then
              room:askToUseRealCard(target, {
                pattern = cards,
                skill_name = skel.name,
                extra_data = {
                  bypass_times = true,
                  extraUse = true,
                  expand_pile = cards,
                },
                cancelable = false,
              })
            end
            if banner[name][1] ~= 4 then
              banner[name] = nil
              room:removePlayerMark(target, "@rmt__feibi", 1)
              room:setBanner("rmt__feibi_combos", banner)
            end
          end
        end
      end
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  --[].连招条件序号 1.连招条件（1~4） 2.是否已满足（1/2) 3.连招技的拥有者（返回ID）
  local banner = player.room:getBanner("rmt__feibi_combos") or {}
  local add = true
  for loop = 1, 10086, 1 do
    local name = "rmt__feibi"..loop
    local infos = player.room:getBanner("rmt__feibi_combos")
    if infos and infos[name] and infos[name][3] == player.id then --如果已有聂隐娘的记录，则不触发后续添加连招
      add = false
      break
    end
  end
  local name
  if add then
    local x = 0
      for i = 1, 10086, 1 do
        if banner["rmt__feibi"..tostring(i)] == nil then
          name = "rmt__feibi"..tostring(i)
          banner[name] = {
            4, -- 1:基本牌 2:锦囊牌 3:装备牌 4:【杀】
            1, -- 1:未满足 2:已满足
            player.id,
          }
          player.room:setBanner("rmt__feibi_combos", banner)
          x = x + 1
          if x > 0 then break end
        end
      end
    player.room:setBanner("rmt__feibi_combos", banner)
    player.room:setPlayerMark(player, "@rmt__feibi", 1)
  end
end)

--说实话这个机制触发失去有点危险，要不哪个神人可以判断一下失去技能的原因
--非隐匿状态下执行清理进度
skel:addLoseEffect(function (self, player, is_death)
  if player:getMark("__hidden_general") == 0 and player:getMark("__hidden_deputy") == 0 then
    for loop = 1, 10086, 1 do 
      local name = "rmt__feibi"..loop
      local infos = player.room:getBanner("rmt__feibi_combos")
      if infos and infos[name] and infos[name][3] == player.id then
        infos[name] = nil
        player.room:setBanner("rmt__feibi_combos", infos)
      end
    end
    player.room:setPlayerMark(player, "@rmt__feibi", 0)
  end
end)

Fk:loadTranslationTable{
  ["rmt__feibi"] = "飞匕",
  [":rmt__feibi"] = "连招技（【杀】），你可于此牌结算后隐匿，若如此做，跳过你下个弃牌阶段，将你使用的下一张牌的类型加入连招条件；"..
  "若类型为装备牌，你登场并倒序视为使用其余连招条件，最后清除所有非【杀】条件，其中为类型的条件改为你视为使用一张该类型的基本牌或普通锦囊牌。",

  ["@rmt__feibi"] = "飞匕",
  ["#rmt__feibi_skip"] = "飞匕",
  ["@@rmt__feibi_add"] = "飞匕 添加条件",
  ["@@rmt__feibi_skip"] = "飞匕 跳弃",

  ["$rmt__feibi1"] = "飞剑蹑罡风，斫得云一朵。",
  ["$rmt__feibi2"] = "鱼肠数寸锋，座上流将血。",
}

return skel
